package ex1.game;

import java.util.Random;

import ex1.ga.Chromosome;
import ex1.game.players.NaivePlayer;
import ex1.game.players.NaiveProberPlayer;
import ex1.game.players.RandomPlayer;
import ex1.game.players.TitForTatPlayer;
import ex1.game.players.TruePeaceMaker;

public class PrisonersDilemmaGame {
	
	final private int NUM_OF_ROUNDS = 1000;
	final private int CC_PAYOFF =  3;
	final private int CD_PAYOFF =  0;
	final private int DC_PAYOFF =  5;
	final private int DD_PAYOFF =  1;
	
	private Player p1;
	private Player p2;
	private int p1Payoff, p2Payoff;	
	private Action p1LastAction = null;
	private Action p2LastAction = null;

	public PrisonersDilemmaGame(Player p1,Player p2)
	{
		this.p1 = p1;
		this.p2 = p2;
	}

	public int runGame()
	{	
		for(int i = 0; i < NUM_OF_ROUNDS; i++)
		{
			runSingleIteration();
		}
		
		return p1Payoff;
	}
	
	private void runSingleIteration()
	{
		Action tempP1 = this.p1LastAction;
		this.p1LastAction = p1.playGame(this, p2LastAction);
		this.p2LastAction = p2.playGame(this, tempP1);
		p1Payoff += this.getPayoff(p1LastAction, p2LastAction);
		p2Payoff += this.getPayoff(p2LastAction, p1LastAction);
		
	
	}
	
	public int getPayoff(Action myAction, Action opponentAction)
	{
		if (myAction == Action.Cooperate && opponentAction == Action.Cooperate)	return CC_PAYOFF;
		if (myAction == Action.Cooperate && opponentAction == Action.Deffect)	return CD_PAYOFF;
		if (myAction == Action.Deffect && opponentAction == Action.Cooperate)	return DC_PAYOFF;
		if (myAction == Action.Deffect && opponentAction == Action.Deffect)	return DD_PAYOFF;
		
		throw new Error("Bad action(s): myAction=" + myAction + ", opponentAction=" + opponentAction);
	}
	

}
